﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChucNang_Admin_Admin_CapNhatPhanLoai : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int id = int.Parse(Request.QueryString.Get("Id"));
        DataDSTableAdapters.PHANLOAITableAdapter pladapter = new DataDSTableAdapters.PHANLOAITableAdapter();
        DataDS.PHANLOAIDataTable pltable = pladapter.PHANLOAI_LayTheoId(id);
        
        
        if (!this.IsPostBack)
        {
            txtTenPhanLoai.Text = pltable.Rows[0]["TenPhanLoai"].ToString();
            imgHinhAnh.ImageUrl = pltable.Rows[0]["HinhAnh"].ToString();
            
            
        }
    }
    protected void btnTroVe_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/ChucNang/Admin/Admin_XemDanhSachPhanLoai.aspx");

    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        int id = int.Parse(Request.QueryString.Get("Id"));
        DataDSTableAdapters.PHANLOAITableAdapter pladapter = new DataDSTableAdapters.PHANLOAITableAdapter();
        int? kq = 0;
        string imageFolder = ConfigurationSettings.AppSettings["IMAGE_FOLDER"];
        if (fileuploadHinhAnh.FileName == "")
        {
            pladapter.PHANLOAI_CapNhatPhanLoai(id, txtTenPhanLoai.Text, imgHinhAnh.ImageUrl, ref kq);

        }
        else
        {
            pladapter.PHANLOAI_CapNhatPhanLoai(id, txtTenPhanLoai.Text, imageFolder + fileuploadHinhAnh.FileName, ref kq);
            string strFilename = Server.MapPath(imageFolder + fileuploadHinhAnh.FileName);
            fileuploadHinhAnh.PostedFile.SaveAs(strFilename);
        }
        //cap nhat thuoc tinh
        DataDSTableAdapters.PHANLOAI_THUOCTINHTableAdapter plttAd = new DataDSTableAdapters.PHANLOAI_THUOCTINHTableAdapter();
        DataDS.PHANLOAI_THUOCTINHDataTable plttTable = plttAd.PHANLOAI_THUOCTINH_LayTheoIdPhanLoai(id);

        for (int i = 0; i < plttTable.Rows.Count; i++ )
        {
            int idThuocTinh = int.Parse(Request.Form.Get("select_"+ plttTable[i].Id));
            plttAd.PHANLOAI_THUOCTINH_CapNhatIdThuocTinhTheoId(plttTable[i].Id, idThuocTinh);
        }

            if (kq == 0)
                Response.Write("<script>alert('Cập nhật thành công');location.href='Admin_XemDanhSachPhanLoai.aspx';</script>");
            else
                Response.Write("<script>alert('Cập nhật không thành công');location.href='Admin_XemDanhSachPhanLoai.aspx';</script>");

    }
    
}
